'use strict';
app.controller('modulesAdvertisementCtrl',['$scope','$state','$stateParams','$modal','modulesAdvertisementService','utilService','toaster',function($scope,$state,$stateParams,$modal,modulesAdvertisementService,utilService,toaster){
        $scope.modulesId=$stateParams.modulesId;

        $scope.modulesAdvertiseListVO={
                modulesId:$scope.modulesId
        };

        $scope.findPageAll=function(){
                utilService.queryForPagingList($scope,{
                        url:$scope.app.domain + 'modules/advertise',
                        params:$scope.modulesAdvertiseListVO
                }).then(function(result){
                });
        }

        $scope.findPageAll();


        //上下架
        $scope.changeStatus= function(id){
                $scope.advertise = {
                        id:id
                };
                modulesAdvertisementService.changeStatus($scope).then(function(result){
                        switch (result.status){
                                case 200:
                                        toaster.pop('success', "提示", "操作成功");
                                        $scope.findPageAll();
                                        break;
                                case 634:
                                        toaster.pop('waiting', "提示", result.message);
                                        break;
                                default :
                                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                                        break
                        }
                });
        }

        //置顶
        $scope.toTop= function(id){
                $scope.advertise = {
                        id:id
                };
                modulesAdvertisementService.toTop($scope).then(function(result){
                        switch (result.status){
                                case 200:
                                        toaster.pop('success', "提示", "操作成功");
                                        $scope.findPageAll();
                                        break;
                                case 634:
                                        toaster.pop('waiting', "提示", result.message);
                                        break;
                                default :
                                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                                        break
                        }
                });
        }

        //删除
        $scope.delModulesAdvertise = function(modulesAdvertiseId,status){
                var param = {
                        modulesAdvertiseId:modulesAdvertiseId
                };
                // query data.
                if(status==1){
                        openModal("该广告正在被使用，是否继续进行删除？");
                }else{
                        openModal("你是否要删除该广告？");
                }

                function openModal(msg){
                        utilService.modal($modal, $scope, {
                                title:"删除广告",
                                controller:"deleteModulesAdvertiseCtrl",
                                message:msg,
                                data:param
                        });
                }
        }


}]);


app.controller('deleteModulesAdvertiseCtrl', ['$scope', '$modalInstance','$state','modulesAdvertisementService','toaster','modalData',
        function($scope, $modalInstance,$state,modulesAdvertisementService,toaster,modalData) {
                $scope.modal = modalData;
                $scope.modulesAdvertise = {
                        modulesAdvertiseId: modalData.data.modulesAdvertiseId
                };
                $scope.isDelModal=true;

                //确认删除
                $scope.ok = function(){
                        if(!$scope.isDelModal){
                                return;
                        }
                        $scope.isDelModal = false;
                        modulesAdvertisementService.del($scope).then(function(result){
                                switch (result.status){
                                        case 200:
                                                $modalInstance.dismiss('success');
                                                toaster.pop('success', "提示", "操作成功");
                                                $scope.isDelModal=true;
                                                $scope.findPageAll();
                                                break;
                                        case 634:
                                        case 660:
                                                toaster.pop('waiting', "提示", result.message);
                                                $scope.isDelModal=true;
                                                break;
                                        default :
                                                toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                                                break
                                }
                        });
                }
                //取消弹窗
                $scope.cancel = function () {
                        $modalInstance.dismiss('cancel');
                };
        }
]);